﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.CompilerServices;

using ConstructionBidOnline.DAL;
using ConstructionBidOnline.DAL.dsGeographicLocationTableAdapters;
using ConstructionBidOnline.Common;

namespace ConstructionBidOnline.Biz
{
    /// <summary>
    /// Country Business Object
    /// </summary>
    public class Country : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected Country() { }

        #endregion

        #region Public Properties

        #region Instance Properties

        /// <summary>
        /// PKID of the Country.
        /// </summary>
        public int LU_CountryID
        {
            get { return _CountryID; }
            set
            {
                if (_CountryID != value)
                {
                    if (OnPropertyChanging("LU_CountryID", _CountryID, value))
                    {
                        int oldValue = _CountryID;
                        _CountryID = value;

                        OnPropertyChanged("LU_CountryID", oldValue, value);
                    }
                }
            }
        }  private int _CountryID = -1;


        /// <summary>
        /// CountryName.
        /// </summary>
        public string CountryName
        {
            get { return _countryName; }
            set
            {
                if (_countryName != value)
                {
                    if (OnPropertyChanging("CountryName", _countryName, value))
                    {
                        string oldValue = _countryName;
                        _countryName = value;
                        OnPropertyChanged("CountryName", oldValue, value);
                    }
                }
            }
        }  private string _countryName = null;



        /// <summary>
        /// CountryCode.
        /// </summary>
        public string CountryCode
        {
            get { return _countryCode; }
            set
            {
                if (_countryCode != value)
                {
                    if (OnPropertyChanging("CountryCode", _countryCode, value))
                    {
                        string oldValue = _countryCode;
                        _countryCode = value;
                        OnPropertyChanged("CountryCode", oldValue, value);
                    }
                }
            }
        }  private string _countryCode = null;



        /// <summary>
        /// Version.
        /// </summary>
        public string Version
        {
            get { return _version; }
            set
            {
                if (_version != value)
                {
                    if (OnPropertyChanging("Version", _version, value))
                    {
                        string oldValue = _version;
                        _version = value;
                        OnPropertyChanged("Version", oldValue, value);
                    }
                }
            }
        }  private string _version = null;


        /// <summary>
        /// True iff the Admin has made this Country Active (i.e. to be used in the application).
        /// </summary>
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                if (_isActive != value)
                {
                    if (OnPropertyChanging("IsActive", _isActive, value))
                    {
                        bool oldValue = _isActive;
                        _isActive = value;
                        OnPropertyChanged("IsActive", oldValue, value);
                    }
                }
            }
        }  private bool _isActive = false;

        #endregion

        #endregion

        #region Public Methods

        #region GetAllCountry

        /// <summary>
        /// Returns a collection of all Country.
        /// </summary>
        public static CountryCollection GetAllCountry()
        {
            CountriesTableAdapter adapter = new CountriesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsGeographicLocation.CountriesDataTable tbl = adapter.GetData(null);

            CountryCollection list = new CountryCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsGeographicLocation.CountriesRow row in tbl.Rows)
                {
                    Country country = new Country();
                    RelationalToObject(country, row);
                    list.Add(country);
                }
            }

            return (list);
        }

        #endregion

        //#region GetCountryByID

        ///// <summary>
        ///// Returns an Country object for the specified 'CountryID' or null if no Country with the specified parameter(s) exists.
        ///// </summary>
        //public static Country GetCountryByID(int Countryid)
        //{
        //    CountrysTableAdapter adapter = new CountrysTableAdapter();
        //    adapter.Connection = AppSettings.GetDbConn();
        //    dsCountry.CountrysDataTable tbl = adapter.GetData(Countryid);

        //    if (tbl.Rows.Count == 1)
        //    {
        //        Country Country = new Country();
        //        RelationalToObject(Country, tbl.Rows[0]);
        //        return (Country);
        //    }
        //    else if (tbl.Rows.Count > 1)
        //        throw new ApplicationException("Database integrity error: Specified 'userLogin' returned more than one User record.");

        //    return (null);
        //}

        //#endregion

        #region Validate

        public override bool Validate()
        {
            return true;

        }

        #endregion

        #endregion

        #region Privates


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more Country objects.
    /// </summary>

    public class CountryCollection : List<Country>
    {
        #region Types

        public class UserComparer : IComparer<Country>
        {
            public int Compare(Country c1, Country c2)
            {
                return (String.Compare(c1.CountryName, c2.CountryName));
            }
        }

        #endregion

        #region Public Methods

        #region FindByCountryID

        public Country FindByCountryID(int countryID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].LU_CountryID == countryID)
                    return (this[ndx]);

            return (null);
        }

        #endregion
        
        #endregion
    }



    /// <summary>
    /// State Business Object
    /// </summary>
    public class State : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected State() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// PKID of the State.
        /// </summary>
        public int LU_StateID
        {
            get { return _stateID; }
            set
            {
                if (_stateID != value)
                {
                    if (OnPropertyChanging("LU_StateID", _stateID, value))
                    {
                        int oldValue = _stateID;
                        _stateID = value;

                        OnPropertyChanged("LU_StateID", oldValue, value);
                    }
                }
            }
        }  private int _stateID = -1;


        /// <summary>
        /// Referenced PKID of the Country.
        /// </summary>
        public int LU_CountryID
        {
            get { return _CountryID; }
            set
            {
                if (_CountryID != value)
                {
                    if (OnPropertyChanging("LU_CountryID", _CountryID, value))
                    {
                        int oldValue = _CountryID;
                        _CountryID = value;

                        OnPropertyChanged("LU_CountryID", oldValue, value);
                    }
                }
            }
        }  private int _CountryID = -1;


        /// <summary>
        /// StateName.
        /// </summary>
        public string StateName
        {
            get { return _stateName; }
            set
            {
                if (_stateName != value)
                {
                    if (OnPropertyChanging("StateName", _stateName, value))
                    {
                        string oldValue = _stateName;
                        _stateName = value;
                        OnPropertyChanged("StateName", oldValue, value);
                    }
                }
            }
        }  private string _stateName = null;


        /// <summary>
        /// StateCode.
        /// </summary>
        public string StateCode
        {
            get { return _stateCode; }
            set
            {
                if (_stateCode != value)
                {
                    if (OnPropertyChanging("StateCode", _stateCode, value))
                    {
                        string oldValue = _stateCode;
                        _stateCode = value;
                        OnPropertyChanged("StateCode", oldValue, value);
                    }
                }
            }
        }  private string _stateCode = null;


        /// <summary>
        /// True iff the Admin has made this Country Active (i.e. to be used in the application).
        /// </summary>
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                if (_isActive != value)
                {
                    if (OnPropertyChanging("IsActive", _isActive, value))
                    {
                        bool oldValue = _isActive;
                        _isActive = value;
                        OnPropertyChanged("IsActive", oldValue, value);
                    }
                }
            }
        }  private bool _isActive = false;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty JobCategory object.
        /// </summary>
        public static State CreateNew()
        {
            State obj = new State();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;

            obj.IsActive = true;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static State CreateNew(StateCollection collection)
        {
            State obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (State State in collection)
            {
                if (State.LU_StateID < minVal)
                    minVal = State.LU_StateID;
            }
            obj.LU_StateID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllStates

        /// <summary>
        /// Returns a collection of all States.
        /// </summary>
        public static StateCollection GetAllStates()
        {
            StatesTableAdapter adapter = new StatesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsGeographicLocation.StatesDataTable tbl = adapter.GetData(null);

            StateCollection list = new StateCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsGeographicLocation.StatesRow row in tbl.Rows)
                {
                    State state = new State();
                    RelationalToObject(state, row);
                    list.Add(state);
                }
            }

            return (list);
        }

        #endregion

        #region GetStateByStateName

        /// <summary>
        /// Returns an State object for the specified 'StateName' or null if no State with the specified parameter(s) exists.
        /// </summary>
        public static State GetStateByStateName(string statename)
        {
            StatesTableAdapter adapter = new StatesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsGeographicLocation.StatesDataTable tbl = adapter.GetDataByStateName(statename);

            if (tbl.Rows.Count == 1)
            {
                State state = new State();
                RelationalToObject(state, tbl.Rows[0]);
                return (state);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'userLogin' returned more than one User record.");

            return (null);
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (LU_StateID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("StateID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // UserName is required.
                if ((StateName == null) || (StateName.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("StateName", "StateName is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified State.
        /// </summary>
        public static void Update(State obj)
        {
            bool updateState = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateState = (obj.ObjState != ObjectState.Unchanged);

                if (!updateState)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsGeographicLocation.StatesDataTable tbl = null;
            if (updateState)
            {
                StatesTableAdapter adapter = new StatesTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsGeographicLocation.StatesDataTable();
                dsGeographicLocation.StatesRow row = tbl.NewStatesRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #endregion

        #region Privates None


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more State objects.
    /// </summary>

    public class StateCollection : List<State>
    {
        #region Types

        public class UserComparer : IComparer<State>
        {
            public int Compare(State s1, State s2)
            {
                return (String.Compare(s1.StateName, s2.StateName));
            }
        }

        #endregion

        #region Public Methods

        #region FindByStateID

        public State FindByStateID(int stateID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].LU_StateID == stateID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #region FindByStateName

        public State FindByStateName(string statename)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].StateName == statename)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion
    }




    /// <summary>
    /// City Business Object
    /// </summary>
    public class City : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected City() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// PKID of the City.
        /// </summary>
        public int LU_CityID
        {
            get { return _cityID; }
            set
            {
                if (_cityID != value)
                {
                    if (OnPropertyChanging("LU_CityID", _cityID, value))
                    {
                        int oldValue = _cityID;
                        _cityID = value;

                        OnPropertyChanged("LU_CityID", oldValue, value);
                    }
                }
            }
        }  private int _cityID = -1;


        /// <summary>
        /// StateName.
        /// </summary>
        public string StateName
        {
            get { return _stateName; }
            set
            {
                if (_stateName != value)
                {
                    if (OnPropertyChanging("StateName", _stateName, value))
                    {
                        string oldValue = _stateName;
                        _stateName = value;
                        OnPropertyChanged("StateName", oldValue, value);
                    }
                }
            }
        }  private string _stateName = null;



        /// <summary>
        /// CityName.
        /// </summary>
        public string CityName
        {
            get { return _cityName; }
            set
            {
                if (_cityName != value)
                {
                    if (OnPropertyChanging("CityName", _cityName, value))
                    {
                        string oldValue = _cityName;
                        _cityName = value;
                        OnPropertyChanged("CityName", oldValue, value);
                    }
                }
            }
        }  private string _cityName = null;


        /// <summary>
        /// True iff the Admin has made this Country Active (i.e. to be used in the application).
        /// </summary>
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                if (_isActive != value)
                {
                    if (OnPropertyChanging("IsActive", _isActive, value))
                    {
                        bool oldValue = _isActive;
                        _isActive = value;
                        OnPropertyChanged("IsActive", oldValue, value);
                    }
                }
            }
        }  private bool _isActive = false;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty JobCategory object.
        /// </summary>
        public static City CreateNew()
        {
            City obj = new City();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;
            obj.IsActive = true;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static City CreateNew(CityCollection collection)
        {
            City obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (City City in collection)
            {
                if (City.LU_CityID < minVal)
                    minVal = City.LU_CityID;
            }
            obj.LU_CityID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllCities

        /// <summary>
        /// Returns a collection of all Cities.
        /// </summary>
        public static CityCollection GetAllCities()
        {
            CitiesTableAdapter adapter = new CitiesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsGeographicLocation.CitiesDataTable tbl = adapter.GetData(null);

            CityCollection list = new CityCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsGeographicLocation.CitiesRow row in tbl.Rows)
                {
                    City city = new City();
                    RelationalToObject(city, row);
                    list.Add(city);
                }
            }

            return (list);
        }

        #endregion

        #region GetAllCitiesStateWise

        /// <summary>
        /// Returns a collection of all Cities.
        /// </summary>
        public static CityCollection GetAllCitiesStateWise(string statename)
        {
            CitiesTableAdapter adapter = new CitiesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsGeographicLocation.CitiesDataTable tbl = adapter.GetDataStateWise(statename);

            CityCollection list = new CityCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsGeographicLocation.CitiesRow row in tbl.Rows)
                {
                    City city = new City();
                    RelationalToObject(city, row);
                    list.Add(city);
                }
            }

            return (list);
        }

        #endregion

        #region GetCitiesByCityName

        /// <summary>
        /// Returns an City object for the specified 'CityName' or null if no City with the specified parameter(s) exists.
        /// </summary>
        public static City GetCitiesByCityName(string cityname)
        {
            CitiesTableAdapter adapter = new CitiesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsGeographicLocation.CitiesDataTable tbl = adapter.GetDataByCityName(cityname);

            if (tbl.Rows.Count == 1)
            {
                City city = new City();
                RelationalToObject(city, tbl.Rows[0]);
                return (city);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'userLogin' returned more than one User record.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified City.
        /// </summary>
        public static void Update(City obj)
        {
            bool updateCity = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updateCity = (obj.ObjState != ObjectState.Unchanged);

                if (!updateCity)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsGeographicLocation.CitiesDataTable tbl = null;
            if (updateCity)
            {
                CitiesTableAdapter adapter = new CitiesTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsGeographicLocation.CitiesDataTable();
                dsGeographicLocation.CitiesRow row = tbl.NewCitiesRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (LU_CityID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("CityID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // UserName is required.
                if ((CityName == null) || (CityName.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("CityName", "CityName is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #endregion

        #region Privates None


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more City objects.
    /// </summary>

    public class CityCollection : List<City>
    {
        #region Types

        public class UserComparer : IComparer<City>
        {
            public int Compare(City c1, City c2)
            {
                return (String.Compare(c1.CityName, c2.CityName));
            }
        }

        #endregion

        #region Public Methods

        #region FindByCityID

        public City FindByCityID(int cityID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].LU_CityID == cityID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #region FindByStateName

        public City FindByStateName(string statename)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].StateName == statename)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #region FindByCityName

        public City FindByCityName(string cityname)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].CityName == cityname)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion
    }



    /// <summary>
    /// PostalCode Business Object
    /// </summary>
    public class PostalCodes : BOBase
    {
        #region Construction / Disposal

        /// <summary>
        /// Default constructor.
        /// </summary>
        protected PostalCodes() { }

        #endregion

        #region Public Properties

        #region Instance Properties


        /// <summary>
        /// PKID of the PostalCode.
        /// </summary>
        public int LU_PostalCodeID
        {
            get { return _postalcodeID; }
            set
            {
                if (_postalcodeID != value)
                {
                    if (OnPropertyChanging("LU_PostalCodeID", _postalcodeID, value))
                    {
                        int oldValue = _postalcodeID;
                        _postalcodeID = value;

                        OnPropertyChanged("LU_PostalCodeID", oldValue, value);
                    }
                }
            }
        }  private int _postalcodeID = -1;


        /// <summary>
        /// PostalCode.
        /// </summary>
        public string PostalCode
        {
            get { return _postalcode; }
            set
            {
                if (_postalcode != value)
                {
                    if (OnPropertyChanging("PostalCode", _postalcode, value))
                    {
                        string oldValue = _postalcode;
                        _postalcode = value;
                        OnPropertyChanged("PostalCode", oldValue, value);
                    }
                }
            }
        }  private string _postalcode = null;



        /// <summary>
        /// StateCode.
        /// </summary>
        public string StateCode
        {
            get { return _statecode; }
            set
            {
                if (_statecode != value)
                {
                    if (OnPropertyChanging("StateCode", _statecode, value))
                    {
                        string oldValue = _statecode;
                        _statecode = value;
                        OnPropertyChanged("StateCode", oldValue, value);
                    }
                }
            }
        }  private string _statecode = null;


        /// <summary>
        /// True iff the Admin has made this Country Active (i.e. to be used in the application).
        /// </summary>
        public bool IsActive
        {
            get { return _isActive; }
            set
            {
                if (_isActive != value)
                {
                    if (OnPropertyChanging("IsActive", _isActive, value))
                    {
                        bool oldValue = _isActive;
                        _isActive = value;
                        OnPropertyChanged("IsActive", oldValue, value);
                    }
                }
            }
        }  private bool _isActive = false;

        #endregion

        #endregion

        #region Public Methods

        #region CreateNew

        /// <summary>
        /// Creates a new, empty JobCategory object.
        /// </summary>
        public static PostalCodes CreateNew()
        {
            PostalCodes obj = new PostalCodes();

            // Switch state to Added prior to initializing property values.
            obj.ObjState = ObjectState.Added;
            obj.IsActive = true;
            return (obj);
        }


        [MethodImpl(MethodImplOptions.Synchronized)]
        public static PostalCodes CreateNew(PostalCodesCollection collection)
        {
            PostalCodes obj = CreateNew();

            // Get next/unique ID for the new object.
            int minVal = 0;
            foreach (PostalCodes PostalCodes in collection)
            {
                if (PostalCodes.LU_PostalCodeID < minVal)
                    minVal = PostalCodes.LU_PostalCodeID;
            }
            obj.LU_PostalCodeID = --minVal;
            collection.Add(obj);

            return (obj);
        }

        #endregion

        #region GetAllPostalCodes

        /// <summary>
        /// Returns a collection of all Cities.
        /// </summary>
        public static PostalCodesCollection GetAllPostalCodes()
        {
            PostalCodesTableAdapter adapter = new PostalCodesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsGeographicLocation.PostalCodesDataTable tbl = adapter.GetData(null);

            PostalCodesCollection list = new PostalCodesCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsGeographicLocation.PostalCodesRow row in tbl.Rows)
                {
                    PostalCodes postalcode = new PostalCodes();
                    RelationalToObject(postalcode, row);
                    list.Add(postalcode);
                }
            }

            return (list);
        }

        #endregion

        #region GetAllPostalCodesByStateCode

        /// <summary>
        /// Returns a collection of all Cities.
        /// </summary>
        public static PostalCodesCollection GetAllPostalCodesByStateCode(string statecode)
        {
            PostalCodesTableAdapter adapter = new PostalCodesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();

            dsGeographicLocation.PostalCodesDataTable tbl = adapter.GetDataByStateCode(statecode);

            PostalCodesCollection list = new PostalCodesCollection();

            if (tbl.Rows.Count > 0)
            {
                foreach (dsGeographicLocation.PostalCodesRow row in tbl.Rows)
                {
                    PostalCodes postalcode = new PostalCodes();
                    RelationalToObject(postalcode, row);
                    list.Add(postalcode);
                }
            }

            return (list);
        }

        #endregion

        #region GetPostalCodesByPostalCode

        /// <summary>
        /// Returns an PostalCodes object for the specified 'PostalCodes' or null if no postalCode with the specified parameter(s) exists.
        /// </summary>
        public static PostalCodes GetPostalCodesByPostalCode(string postalCode)
        {
            PostalCodesTableAdapter adapter = new PostalCodesTableAdapter();
            adapter.Connection = AppSettings.GetDbConn();
            dsGeographicLocation.PostalCodesDataTable tbl = adapter.GetDataByPostalCode(postalCode);

            if (tbl.Rows.Count == 1)
            {
                PostalCodes postalcodes = new PostalCodes();
                RelationalToObject(postalcodes, tbl.Rows[0]);
                return (postalcodes);
            }
            else if (tbl.Rows.Count > 1)
                throw new ApplicationException("Database integrity error: Specified 'userLogin' returned more than one User record.");

            return (null);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the specified PostalCodes.
        /// </summary>
        public static void Update(PostalCodes obj)
        {
            bool updatePostalCodes = false;

            // Do not update invalid objects.
            if (!obj.Validate())
            {
                StringBuilder sb = new StringBuilder(2048);
                sb.Append("The object being updated has one or more invalid properties; the Update() operation for this object has been cancelled.\nError(s):");
                foreach (ValidationError err in obj.Errors)
                    sb.Append("\n* " + err.Element + ": " + err.Message);

                throw new InvalidObjectException(sb.ToString());
            }
            else
            {
                updatePostalCodes = (obj.ObjState != ObjectState.Unchanged);

                if (!updatePostalCodes)
                    return;
            }

            ObjectState originalObjectState = obj.ObjState;

            dsGeographicLocation.PostalCodesDataTable tbl = null;
            if (updatePostalCodes)
            {
                PostalCodesTableAdapter adapter = new PostalCodesTableAdapter();
                adapter.Connection = AppSettings.GetDbConn();

                tbl = new dsGeographicLocation.PostalCodesDataTable();
                dsGeographicLocation.PostalCodesRow row = tbl.NewPostalCodesRow();
                ObjectToRelational(obj, tbl, row);

                adapter.Update(tbl);

                if (originalObjectState == ObjectState.Deleted)
                    obj = null;
                else RelationalToObject(obj, tbl.Rows[0]);
            }
        }

        #endregion

        #region Validate

        /// <summary>
        /// Validates the object based on its properties at the time of the call; updates the Errors property.
        /// </summary>
        public override bool Validate()
        {
            Errors.Clear();

            if (ObjState == ObjectState.Unchanged)
            {
                // By definition, unchanged objects are valid.
                return (true);
            }
            else if (ObjState == ObjectState.Uninitialized)
            {
                Errors.Add(new ValidationError("object", "Object is uninitialized.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else if ((ObjState == ObjectState.Deleted) && (LU_PostalCodeID <= 0))
            {
                // Deleted items must have 'real' (i.e. non-negative, non-zero) key(s) to be valid.
                Errors.Add(new ValidationError("PostalCodesID", "Deleted object has an invalid PKID.", ValidationError.Severity.Error));

                // No need to proceed past this error.
                return (false);
            }
            else  // ObjState == Added | Modified
            {

                // UserName is required.
                if ((PostalCode == null) || (PostalCode.Trim().Length == 0))
                {
                    Errors.Add(new ValidationError("PostalCode", "PostalCode is required.", ValidationError.Severity.Error));
                }
                else if (ObjState == ObjectState.Added)
                {

                }

            }

            return (Errors.Count == 0);
        }

        #endregion

        #endregion

        #region Privates None


        #endregion

    }

    /// <summary>
    /// Collection of 0 or more PostalCodes objects.
    /// </summary>

    public class PostalCodesCollection : List<PostalCodes>
    {
        #region Types

        public class UserComparer : IComparer<PostalCodes>
        {
            public int Compare(PostalCodes pc1, PostalCodes pc2)
            {
                return (String.Compare(pc1.PostalCode, pc2.PostalCode));
            }
        }

        #endregion

        #region Public Methods

        #region FindByPostalCodeID

        public PostalCodes FindByPostalCodeID(int postalcodeID)
        {
            for (int ndx = 0; ndx < this.Count; ndx++)
                if (this[ndx].LU_PostalCodeID == postalcodeID)
                    return (this[ndx]);

            return (null);
        }

        #endregion

        #endregion
    }
}
